"""
逻辑回归：分类模型，主要解决二分类问题
本质：线性回归模型的结果通过sigmoid函数映射到0-1之间 -> 概率 -> 二分类问题
"""
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression


def log_demo():
    # 1.读取数据
    data = load_breast_cancer()
    x, y = data.data, data.target

    # 2.数据集划分
    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=22)

    # 3. 标准化'
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.transform(x_test)

    # 4. 逻辑回归估计器流程
    estimator = LogisticRegression()
    estimator.fit(x_train, y_train)

    print("权重：", estimator.coef_)
    print("偏置：", estimator.intercept_)

    # 5. 模型评估
    print("准确率：", estimator.score(x_test, y_test))

    # 6. 预测新数据
    data_new = [x_test[0]]    # 需要是二维数组
    print("模型对新数据的预测结果：", estimator.predict(data_new))
    print("模型对新数据的预测概率：", estimator.predict_proba(data_new))


if __name__ == '__main__':
    log_demo()